package dth.bean;

import java.awt.PageAttributes;
import java.sql.Date;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.model.SelectItem;
import javax.management.relation.Role;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.dao.PackageDAO;
import dth.dao.UserDAO;
import dth.dao.UserPackageDAO;
import dth.entity.Package;
import dth.entity.User;
import dth.entity.UserPackage;
import dth.utils.DthUtil;
import antlr.collections.List;

@ManagedBean(name = "packageBean")
public class PackageBean {
	@ManagedProperty(value = "#{loginBean}")
	private LoginBean loginBean;// get session login bean
	private int id = 0;// use to insert delete
	
	private String name;
	private double price;
	private String description;
	private int fkAdminId;
	private java.util.List<Package> listPackage = new ArrayList<Package>();
	private java.util.List<Package> listUserPackage = new ArrayList<Package>();//list package cua 1 user
	
	public java.util.List<Package> getListUserPackage() {
		PackageDAO dao=new PackageDAO();
		System.out.println("---user id on package:"+loginBean.id);
		this.listUserPackage=dao.getListPackageByUserId(LoginBean.id);
		return listUserPackage;
	}

	public void setListUserPackage(java.util.List<Package> listUserPackage) {
		
		this.listUserPackage = listUserPackage;
	}

	public java.util.List<Package> getListPackage() {
		PackageDAO packageDAO = new PackageDAO();
		listPackage=packageDAO.getListPackage();
		if (LoginBean.roleCode == 4) {
			System.out.println("--role="+LoginBean.roleCode);
			/*
			 * check nhung package user dang chon
			 */
			UserPackageDAO userPackageDAO = new UserPackageDAO();
			java.util.List<UserPackage> lisUserPackages = userPackageDAO
					.getListUserPackgeByUserId(LoginBean.id);
			if (lisUserPackages != null && lisUserPackages.size() > 0) {
				System.out.println("------list package user size:"
						+ lisUserPackages.size());
				for (Package p : listPackage) {
					for (UserPackage u : lisUserPackages) {
						if (u.getOwnPackage().getId() == p.getId()) {
							System.out.println("------co 1 package");
							p.setSelected(true);// chon package
						}
					}
				}

			} else
				System.out.println("---------list packge user used is null---");
		}
		return listPackage;
	}
	
	public void setListPackage(java.util.List<Package> listPackages) {
		this.listPackage = listPackages;
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getFkAdminId() {
		return fkAdminId;
	}

	public void setFkAdminId(int fkAdminId) {
		this.fkAdminId = fkAdminId;
	}

	public LoginBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}
	
	

	public PackageBean() {
		// TODO Auto-generated constructor stub
		System.out.println("--package bean");
		Object objectAction = DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:" + objectAction);
		/*
		 * neu nhan request tu page package.xhtml thi xet role=1 moi cho vao
		 */
		System.out.println("---request url:"+DthUtil.getRequestURL());
		if(DthUtil.getRequestURL().contains("package.xhtml")){
			System.out.println("--vao day role:"+LoginBean.roleCode);
			if(LoginBean.roleCode!=1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		
		
		if (objectAction != null) {
			if (objectAction.equals("delete")) {
				Object objectId = DthUtil.getRequest().getParameter(
						"package_id");
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id delete:" + id);
			} else if (objectAction.equals("update")) {
				Object objectId = DthUtil.getRequest().getParameter(
						"package_id");
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id edit:" + id);
				for (Package pack : getListPackage()) {
					if (pack.getId() == id) {
						this.name = pack.getName();
						this.description = pack.getDescription();
						this.price = pack.getPrice();
						this.fkAdminId = pack.getFkAdminId();
					}
				}
				System.out.println("-----name:" + name);
			}

		}
	}

	public void delete() {
		try {
			PackageDAO packageDAO = new PackageDAO();
			int count = packageDAO.delete(id);
			if (count > 0)
				DthUtil.showMessageSuccess();
			else
				DthUtil.showMessageError();
		} catch (Exception ex) {
			DthUtil.showMessageSuccess();
		}
	}

	public void insert() {
		System.out.println("---id=" + id);
		try {
			PackageDAO packageDAO = new PackageDAO();
			Package package1 = new Package();
			package1.setId(id);
			package1.setName(name);
			package1.setPrice(price);
			package1.setFkAdminId(1);// set cung
			package1.setDescription(description);
			java.util.Date date = new java.util.Date();// lay ngay hien tai
			package1.setDateInsert(new Date(date.getTime()));
			int count = 0;
			if (id == 0)
				count = packageDAO.insert(package1);
			else
				count = packageDAO.update(package1);
			if (count > 0)
				DthUtil.showMessageSuccess();
			else
				DthUtil.showMessageError();
		} catch (Exception ex) {
			System.out.println("---------error:" + ex.getMessage());
			DthUtil.showMessageError();
		}
	}

	public void suggestPackage() {
		System.out.println("---sugegest");
		UserPackageDAO userPackageDAO = new UserPackageDAO();
		userPackageDAO.deleteByUserId(loginBean.getId());
		if (loginBean.getRole() == 4) {
			int success = 0;
			for (Package pack : listPackage) {
				System.out.println("--" + pack.isSelected());

				if (pack.isSelected()) {
					UserPackage userPackage = new UserPackage();
					java.util.Date date = new java.util.Date();// lay ngay hien
																// tai
					userPackage.setDateInsert(new Date(date.getTime()));
					User user=new User();
					user.setCustomerId(loginBean.getId());
					
					success = userPackageDAO.insert(userPackage,pack,user);
					System.out.println("----success:" + success);
				}

			}
			if (success > 0) {
				
				DthUtil.showMessage("Message", "Successful sussgest package");
			} else
				DthUtil.showMessage("Message", "Failed sussgest package");
		}
		else
			DthUtil.showMessage("Message", "You must login to suggest");
		
	}
	
}
